package com.tools.web.work;

import com.tools.common.container.JsonPair;
import com.tools.common.object.Note;

/**
 * 响应结果包装枚举类（手动实现）
 * */
@Note("响应结果包装枚举类（手动实现）")
public class ResultCode {

    public static final ResultCode SUCCESS = new ResultCode(2000, "请求成功");
    public static final ResultCode NOT_UPDATE = new ResultCode(2050, "数据重复，不选择更新");
    public static final ResultCode PARAM_ERROR = new ResultCode(4000, "参数验证失败");
    public static final ResultCode NOT_FOUND_TOKEN = new ResultCode(4004, "缺少身份信息");
    public static final ResultCode CACHE_EXPIRED = new ResultCode(4005, "缓存数据已过期");
    public static final ResultCode ACCESS_FAIL = new ResultCode(4006, "访问权限不足");
    public static final ResultCode NOT_YET_OPEN = new ResultCode(4007, "服务尚未到达开放的时间");
    public static final ResultCode ACCESS_LIMIT = new ResultCode(4009, "单 IP 请求限流");
    public static final ResultCode AUTH_PARSE_FAIL = new ResultCode(4110, "身份验证失败，请重新登录");
    public static final ResultCode AUTH_EXPIRED = new ResultCode(4111, "身份信息已过期，请重新登录");
    public static final ResultCode QUOTA_LIMIT = new ResultCode(4118, "业务成功次数达到指定时间的上限");
    public static final ResultCode DISABLED_USER = new ResultCode(4120, "已封禁/锁定用户");
    public static final ResultCode UPDATE_FAIL = new ResultCode(5001, "数据库更新数据失败");
    public static final ResultCode DATA_NOT_FOUND = new ResultCode(5002, "数据库没有查到指定数据");
    public static final ResultCode DATA_NOT_MATCH = new ResultCode(5003, "业务结果不符合预期");
    public static final ResultCode SERVICE_ERROR = new ResultCode(5004, "调第三方接口时失败");


    @Note("响应状态码")
    private final int code;

    @Note("响应信息")
    private final String message;

    private ResultCode(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public JsonPair getResult() {
        return new JsonPair(code, message);
    }

    public JsonPair getResult(String newMessage) {
        return (newMessage == null || newMessage.isEmpty())
                ? new JsonPair(this.code, this.message)
                : new JsonPair(this.code, newMessage);
    }

    public String toJSON() {
        return this.getResult().toJSON();
    }

    public String toJSON(String newMessage) {
        return this.getResult(newMessage).toJSON();
    }

    public String asJSON() {
        return this.getResult().asJSON();
    }

    public String asJSON(String newMessage) {
        return this.getResult(newMessage).asJSON();
    }

    @Override
    public String toString() {
        return "{" +
                "\"code\": " + code +
                ", \"message\": \"" + message + '\"' +
                '}';
    }
}
